package com.wkl.yuyue.utils;
import org.apache.velocity.shaded.commons.io.FilenameUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.processing.FilerException;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Objects;

public class FileUploadUtils {
    private static final int MAX_FILENAME_LENGTH = 100;
    private static final Long MAX_FILE_SIZE = 52428800L;

    public static String fileUpload(String uploadPath, MultipartFile multipartFile) throws IOException {
        try {
            return fileUpload(uploadPath, multipartFile, MimeTypeUtils.DEFAULT_ALLOW_Type);
        } catch (Exception e) {
            throw new IOException(e.getMessage(), e);
        }
    }

    public static String fileUpload(String uploadPath, MultipartFile multipartFile, String[] default_allow_type) throws IOException {
        int fileNameLength = Objects.requireNonNull(multipartFile.getOriginalFilename()).length();
        //文件名限制
        if (fileNameLength > FileUploadUtils.MAX_FILENAME_LENGTH) {
            throw new FilerException("文件名太长");
        }
        //文件大小/类型限制
        limitFileSize(multipartFile, default_allow_type);

        String encodingFileName = encodingFileName(multipartFile);

        String filePath = getFileWithPath(uploadPath, encodingFileName).getAbsolutePath();
        Path path = Paths.get(filePath);
        multipartFile.transferTo(path);
        return getPathFileName(uploadPath, encodingFileName);
    }

    private static String getPathFileName(String uploadPath, String encodingFileName) {
        int index = "D:/leave/uploadPath".length() + 1;
//        String currentPath = StringUtils.substring(uploadPath, index);
        return uploadPath +"/"+ encodingFileName;
    }

    /**
     * 获取带路径的文件
     *
     * @param uploadPath
     * @param fileName
     * @return
     */
    private static File getFileWithPath(String uploadPath, String fileName) {
        File file = new File(uploadPath + File.separator + fileName);
        if (!file.exists()) {
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
        }
        return file;

    }

    /**
     * 编码文件名（日期+）
     *
     * @param multipartFile
     * @return
     */
    private static String encodingFileName(MultipartFile multipartFile) {
        String encodingName = StringUtils.format("{}.{}",
                FilenameUtils.getBaseName(multipartFile.getOriginalFilename()), FileUploadUtils.getExtension(multipartFile));
        return encodingName;
    }

    private static void limitFileSize(MultipartFile multipartFile, String[] default_allow_type){
        Long fileSize = multipartFile.getSize();
        String fileName = multipartFile.getOriginalFilename();
        if (fileSize > FileUploadUtils.MAX_FILE_SIZE) {

        }
        String extension = getExtension(multipartFile);
        //类型支持校验
        if (default_allow_type != null && !assertFileTyle(extension, default_allow_type)) {

        }

    }

    /**
     * 校验拓展名是否被允许上传
     *
     * @param extension
     * @param default_allow_type
     * @return
     */
    private static boolean assertFileTyle(String extension, String[] default_allow_type) {
        if (!StringUtils.isEmpty(extension)) {
            for (String str : default_allow_type) {
                if (str.equalsIgnoreCase(extension)) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 获取拓展名
     *
     * @param multipartFile
     * @return
     */
    private static String getExtension(MultipartFile multipartFile) {
        String extension = FilenameUtils.getExtension(multipartFile.getOriginalFilename());
        if (StringUtils.isEmpty(extension)) {
            return MimeTypeUtils.getExtension(Objects.requireNonNull(multipartFile.getContentType()));
        }
        return extension;
    }
}